﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 表格示例数据
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-10-26 
    /// </summary>
    internal class TableExampleService : DataFactory, TableExampleIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<TableExampleEntity>> GetList()
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<TableExampleEntity>().OrderBy(o => o.ProjectType).ThenBy(o => o.SortCode).ToListAsync();
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="typeId">类型Id</param>
        /// <returns></returns>
        public async Task<PageResult<TableExampleEntity>> GetList(string typeId, PageInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<TableExampleEntity>().And(m => m.ProjectType == typeId);
                var queryParam = input.queryJson.ToObject();
                //关键字（项目编码、项目名称、客户名称）
                if (input.keyword.IsNotEmptyOrNull())
                {
                    var keyword = input.keyword;
                    queryWhere = queryWhere.And(m => m.ProjectCode.Contains(input.keyword) || m.ProjectName.Contains(input.keyword) || m.CustomerName.Contains(input.keyword));
                }
                //标签查询
                if (!queryParam["sign"].IsEmpty())
                {
                    var index = 0;
                    var arraySign = queryParam["sign"].ToString().Split(',');
                    foreach (var item in arraySign)
                    {
                        queryWhere = index == 0 ? queryWhere.And(t => t.Sign.Contains(item)) : queryWhere.Or(t => t.Sign.Contains(item));
                        index++;
                    }
                }
                return await db.GetIQueryable<TableExampleEntity>().Where(queryWhere).OrderBy(t => t.SortCode).GetPageResultAsync(input);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<TableExampleEntity>> GetList(PageInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<TableExampleEntity>();
                var queryParam = input.queryJson.ToObject();
                //关键字（项目编码、项目名称、客户名称）
                if (input.keyword.IsNotEmptyOrNull())
                {
                    var keyword = input.keyword;
                    queryWhere = queryWhere.And(m => m.ProjectCode.Contains(keyword) || m.ProjectName.Contains(keyword) || m.CustomerName.Contains(keyword));
                }
                //标签查询
                if (!queryParam["sign"].IsEmpty())
                {
                    var index = 0;
                    var arraySign = queryParam["sign"].ToString().Split(',');
                    foreach (var item in arraySign)
                    {
                        queryWhere = index == 0 ? queryWhere.And(t => t.Sign.Contains(item)) : queryWhere.Or(t => t.Sign.Contains(item));
                        index++;
                    }
                }
                return await db.GetIQueryable<TableExampleEntity>().Where(queryWhere).OrderByDescending(t => t.RegisterDate).GetPageResultAsync(input);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<TableExampleEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<TableExampleEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(TableExampleEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(TableExampleEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(TableExampleEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

    }
}